﻿using System;
using System.Collections.Generic;
using Lua;
using UnityEngine;

// Token: 0x020001CC RID: 460
public class MicroManager
{
	// Token: 0x060009C2 RID: 2498 RVA: 0x0005854C File Offset: 0x0005674C
	public MicroManager()
	{
		MicroManager.Instance = this;
		this.cameraTransform = Camera.main.transform;
		this.pool = new Queue<Micro>();
	}

	// Token: 0x060009C3 RID: 2499 RVA: 0x000585A1 File Offset: 0x000567A1
	public void AddMicro(Micro micro)
	{
		this.microDictionary.Add(micro.id, micro);
		this.luaMicroDictionary.Add(micro.id, micro);
	}

	// Token: 0x060009C4 RID: 2500 RVA: 0x000585CC File Offset: 0x000567CC
	public void RemoveMicro(int id)
	{
		this.microDictionary.Remove(id);
		this.luaMicroDictionary.Remove(id);
	}

	// Token: 0x060009C5 RID: 2501 RVA: 0x000585E8 File Offset: 0x000567E8
	public void Update()
	{
		this.frame++;
		if (this.frame % 60 != 0)
		{
			return;
		}
		this.cameraPosition = this.cameraTransform.position;
		foreach (KeyValuePair<int, Micro> keyValuePair in this.microDictionary)
		{
			Micro value = keyValuePair.Value;
			if (value && !value.isPlayer)
			{
				this.MicroUpdate(value);
			}
		}
	}

	// Token: 0x060009C6 RID: 2502 RVA: 0x00058690 File Offset: 0x00056890
	public void MicroUpdate(Micro micro)
	{
		UnityEngine.Vector3 position = micro.myTransform.position;
		float num = position.x - this.cameraPosition.x;
		float num2 = position.y - this.cameraPosition.y;
		float num3 = position.z - this.cameraPosition.z;
		float num4 = num * num + num2 * num2 + num3 * num3;
		float num5 = this.maxDistanceBase * micro.myTransform.lossyScale.y;
		bool flag = micro.IsVisible();
		bool flag2 = num4 < num5 * num5;
		if (flag && !flag2)
		{
			micro.Render(false);
		}
		else if (!flag && flag2)
		{
			micro.Render(true);
		}
	}

	// Token: 0x060009C7 RID: 2503 RVA: 0x00058751 File Offset: 0x00056951
	public IDictionary<int, Entity> GetLuaMicroList()
	{
		return this.luaMicroDictionary;
	}

	// Token: 0x060009C8 RID: 2504 RVA: 0x0005875C File Offset: 0x0005695C
	public static Micro GetRandomMicro(EntityBase entity)
	{
		int count = MicroManager.Instance.microDictionary.Count;
		int num = UnityEngine.Random.Range(0, count);
		int num2 = 0;
		Micro micro = null;
		foreach (KeyValuePair<int, Micro> keyValuePair in MicroManager.Instance.microDictionary)
		{
			if (micro == null && keyValuePair.Value.gameObject.activeInHierarchy && (!entity.isMicro || keyValuePair.Value.id != entity.id))
			{
				micro = keyValuePair.Value;
			}
			if (num2 == num)
			{
				if (keyValuePair.Value.gameObject.activeInHierarchy && (!entity.isMicro || keyValuePair.Value.id != entity.id))
				{
					return keyValuePair.Value;
				}
				num2++;
			}
			else
			{
				num2++;
			}
		}
		return micro;
	}

	// Token: 0x060009C9 RID: 2505 RVA: 0x00058880 File Offset: 0x00056A80
	public void AddToPool(Micro micro)
	{
		this.pool.Enqueue(micro);
	}

	// Token: 0x060009CA RID: 2506 RVA: 0x0005888E File Offset: 0x00056A8E
	public int GetPoolCount()
	{
		return this.pool.Count;
	}

	// Token: 0x060009CB RID: 2507 RVA: 0x0005889B File Offset: 0x00056A9B
	public Micro GetFromPool()
	{
		return this.pool.Dequeue();
	}

	// Token: 0x060009CC RID: 2508 RVA: 0x000588A8 File Offset: 0x00056AA8
	public static Micro FindClosestMicro(EntityBase entity, float scale)
	{
		float num = 0.1f * scale;
		float num2 = 100f * scale;
		List<Micro> list = null;
		while (num < num2 && (list == null || list.Count == 0))
		{
			list = MicroManager.CheckRadius(entity, num);
			num *= 2f;
		}
		Micro result = null;
		float num3 = num2;
		for (int i = 0; i < list.Count; i++)
		{
			float magnitude = (entity.myTransform.position - list[i].myTransform.position).magnitude;
			if (magnitude < num3)
			{
				result = list[i];
				num3 = magnitude;
			}
		}
		return result;
	}

	// Token: 0x060009CD RID: 2509 RVA: 0x00058958 File Offset: 0x00056B58
	public static List<Micro> FindMicrosInRadius(EntityBase entity, float radius)
	{
		radius *= entity.Height * 0.625f;
		return MicroManager.CheckRadius(entity, radius);
	}

	// Token: 0x060009CE RID: 2510 RVA: 0x00058980 File Offset: 0x00056B80
	private static List<Micro> CheckRadius(EntityBase entity, float radius)
	{
		Collider[] array = Physics.OverlapSphere(entity.myTransform.position, radius, Layers.crushableMask, QueryTriggerInteraction.Collide);
		List<Micro> list = new List<Micro>();
		foreach (Collider collider in array)
		{
			if (!GlobalPreferences.IgnorePlayer.value || collider.gameObject.layer != Layers.playerLayer)
			{
				Micro componentInParent = collider.gameObject.GetComponentInParent<Micro>();
				if (componentInParent && !componentInParent.isDead && !componentInParent.locked && entity.id != componentInParent.id)
				{
					if (!componentInParent.transform.parent || componentInParent.transform.parent.gameObject.layer != Layers.gtsBodyLayer)
					{
						float num = componentInParent.CheckForOverHeadObjects(entity.Height);
						if (num == 0f || num >= entity.Height)
						{
							if (!componentInParent.IsOnGiantess())
							{
								list.Add(componentInParent);
							}
						}
					}
				}
			}
		}
		return list;
	}

	// Token: 0x04000F49 RID: 3913
	public static MicroManager Instance;

	// Token: 0x04000F4A RID: 3914
	private Dictionary<int, Micro> microDictionary = new Dictionary<int, Micro>();

	// Token: 0x04000F4B RID: 3915
	private Dictionary<int, Entity> luaMicroDictionary = new Dictionary<int, Entity>();

	// Token: 0x04000F4C RID: 3916
	private Queue<Micro> pool;

	// Token: 0x04000F4D RID: 3917
	private UnityEngine.Transform cameraTransform;

	// Token: 0x04000F4E RID: 3918
	private UnityEngine.Vector3 cameraPosition;

	// Token: 0x04000F4F RID: 3919
	private float maxDistanceBase = 500f;

	// Token: 0x04000F50 RID: 3920
	private int frame;
}
